﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Artem.Google.Net;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class NuevoAviso : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["actual_module"] = "NuevoAviso";
        Session["direccionGuardada"] = false;
        Session["lat"] = 0.0;
        Session["long"] = 0.0;

        if (!Page.IsPostBack)
        {
            string region = RegionInfo.CurrentRegion.TwoLetterISORegionName;
            this.region.InnerText += " : " + region;
            fillRubros();
        }
        DefaultRegionMapShow();

    }

    private void fillRubros()
    {
        ArrayList rubros = Persistencia.getInstance().ListaRubro();

        foreach(cRubro rub in rubros)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl a = new HtmlGenericControl("a");
            a.Attributes.Add("href", "#");
            a.InnerHtml = rub.Nombre;
            li.Controls.Add(a);
            inputRubroDDL.Controls.Add(li);
        }
    }

    protected void Direccion_TextChanged(object sender, EventArgs e)
    {
        if (inputDireccionTextBox.Text.Equals(""))
        {
            DefaultRegionMapShow();
            return;
        }

        //LOCALIZACION EN EL MAPA (DIRECCION A LATLONG)
        GeoRequest request = new GeoRequest(inputDireccionTextBox.Text);
        GeoResponse response = request.GetResponse();
        GeoLocation location = response.Results[0].Geometry.Location;
        double latitude = location.Latitude;
        double longitude = location.Longitude;

        
        Session["direccion"] = inputDireccionTextBox.Text;
        Session["lat"] = latitude;
        Session["long"] = longitude;

        //CREACION MAPA
        GoogleMap1.Latitude = latitude;
        GoogleMap1.Longitude = longitude;
        GoogleMap1.MapType = Artem.Google.UI.MapType.Roadmap;
        GoogleMap1.Zoom = 17;

        //MARCADOR 
        Artem.Google.UI.Marker marcador = new Artem.Google.UI.Marker();
        Session["direccion_marker"] = marcador;
        marcador.Position.Latitude = latitude;
        marcador.Position.Longitude = longitude;
        marcador.Info = inputDireccionTextBox.Text.ToUpper().Trim();
        marcador.AutoOpen = true;
        GoogleMap1.Markers.Remove((Artem.Google.UI.Marker)Session["region_marker"]);
        GoogleMap1.Markers.Add(marcador);
        inputDireccionTextBox.Enabled = false;
        if((Boolean)Session["direccionGuardada"] == false)
        {
            AlertaDireccion.Visible = true;
        }
        
        
    }

    protected void PosicionCorrecta_Click(object sender,EventArgs e)
    {
        AlertaDireccion.Visible = false;
        Session["direccionGuardada"] = true;
        Direccion_TextChanged(null, EventArgs.Empty);
    }

    protected void PosicionInCorrecta_Click(object sender, EventArgs e)
    {
        AlertaDireccion.Visible = false;
        inputDireccionTextBox.Text = "";
        Session["direccionGuardada"] = false;
        inputDireccionTextBox.Enabled = true;
        Direccion_TextChanged(null, EventArgs.Empty);
        
    }


    private void DefaultRegionMapShow()
    {
        //DATOS REGION CLIENTE
        string name = RegionInfo.CurrentRegion.DisplayName;
        

        //LOCALIZACION EN EL MAPA (DIRECCION A LATLONG)
        GeoRequest request = new GeoRequest(name);
        GeoResponse response = request.GetResponse();
        GeoLocation location = response.Results[0].Geometry.Location;
        double latitude = location.Latitude;
        double longitude = location.Longitude;


        //CREACION MAPA
        GoogleMap1.Latitude = latitude;
        GoogleMap1.Longitude = longitude;
        GoogleMap1.MapType = Artem.Google.UI.MapType.Roadmap;
        GoogleMap1.Zoom = 4;

        //MARCADOR 
        Artem.Google.UI.Marker marcador = new Artem.Google.UI.Marker();
        Session["region_marker"] = marcador;
        marcador.Position.Latitude = latitude;
        marcador.Position.Longitude = longitude;
        GoogleMap1.Markers.Add(marcador);
 
    }
}